package com.srwl.mytx.tao.av;

import com.srwl.mytx.tao.av.VideoBean.State;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//import com.taobao.av.ui.view.recordline.VideoBean;
//import com.taobao.av.ui.view.recordline.VideoBean.State;
//import com.taobao.av.util.FileUtils;

public class ClipManager {
    private final int MinTime = 200;
    private final ArrayList< OnClipChangeListener> _ClipChangeListenerList = new ArrayList();
    private final ArrayList<Listener> _ListenerList = new ArrayList();
    private final ArrayList<VideoBean> videos = new ArrayList();
    private int _MaxDuration;
    private int _MinDuration;
    private long _Duration;
    private VideoBean _LastClip;

    public ClipManager() {
    }

    public void addClipChangeListener(ClipManager.OnClipChangeListener listener) {
        this._ClipChangeListenerList.add(listener);
    }

    public void addListener(ClipManager.Listener listener) {
        this._ListenerList.add(listener);
    }

    public List<VideoBean> getClipList() {
        return this.videos;
    }

    public void onRecordStarted(VideoBean vb) {
        this._LastClip = vb;
        this._LastClip.setState(State.CAPTURING);
        this.dispatchClipCreate(vb);
    }

    public void onRecordFrame(long partTimes) {
        this._LastClip.videoTimes = partTimes;
        this.dispatchClipChange(this._LastClip);
    }

    public void onRecordPaused() {
        if (this._LastClip != null && this._LastClip.videoTimes >= 0L) {
            VideoBean vb = new VideoBean();
            vb.videoFile = this._LastClip.videoFile;
            vb.videoTimes = this._LastClip.videoTimes;
            vb.setState(State.READY);
            this.videos.add(vb);
            this._LastClip = null;
            this.updateDuration();
            this.dispatchClipChange(vb);
        }
    }

    public void setMaxDuration(int duration) {
        this._MaxDuration = duration;
    }

    public float getMaxDuration() {
        return (float)this._MaxDuration;
    }

    public void setMinDuration(int duration) {
        this._MinDuration = duration;
    }

    public int getMinDuration() {
        return this._MinDuration;
    }

    public boolean isMinDurationReached() {
        return this.getDuration() >= this._MinDuration;
    }

    public boolean isMaxDurationReached() {
        return this.getDuration() >= this._MaxDuration;
    }

    public int getDuration() {
        return (int)(this._Duration + this.getLastClipDuration());
    }

    private long getLastClipDuration() {
        return this._LastClip == null ? 0L : this._LastClip.videoTimes;
    }

    private int getRemainingDuration() {
        return Math.max((int)((long)this._MaxDuration - this._Duration), 0);
    }

    private void updateDuration() {
        long duration = 0L;

        VideoBean item;
        for(Iterator i$ = this.videos.iterator(); i$.hasNext(); duration += item.videoTimes) {
            item = (VideoBean)i$.next();
        }

        this._Duration = duration;
    }

    public VideoBean[] getClipArray() {
        return (VideoBean[])this.videos.toArray(new VideoBean[0]);
    }

    public VideoBean getClip(int ix) {
        return ix == this.videos.size() ? this._LastClip : (VideoBean)this.videos.get(ix);
    }

    public int getClipCount() {
        return (this._LastClip == null ? 0 : 1) + this.videos.size();
    }

    public boolean isEmpty() {
        return this.videos.isEmpty();
    }

    public boolean isUnEmpty() {
        return !this.videos.isEmpty();
    }

    public void removeLastClip() {
        if (!this.videos.isEmpty()) {
            VideoBean last = (VideoBean)this.videos.get(this.videos.size() - 1);
            this.videos.remove(this.videos.size() - 1);
            FileUtils.deleteFile(last.videoFile);
            this.updateDuration();
            this.dispatchClipDelete(last);
        }
    }

    private long getLastClipTime() {
        if (this.videos.isEmpty()) {
            return -1L;
        } else {
            VideoBean last = (VideoBean)this.videos.get(this.videos.size() - 1);
            return last.videoTimes;
        }
    }

    public void clear() {
        this.videos.clear();
        this.updateDuration();
    }

    private void dispatchClipDelete(VideoBean vb) {
        Iterator i$ = this._ListenerList.iterator();

        while(i$.hasNext()) {
            ClipManager.Listener l = (ClipManager.Listener)i$.next();
            l.onClipDelete(this, vb);
        }

    }

    private void dispatchClipCreate(VideoBean vb) {
        Iterator i$ = this._ListenerList.iterator();

        while(i$.hasNext()) {
            ClipManager.Listener l = (ClipManager.Listener)i$.next();
            l.onClipCreate(this, vb);
        }

    }

    private void dispatchClipChange(VideoBean vb) {
        Iterator i$ = this._ClipChangeListenerList.iterator();

        while(i$.hasNext()) {
            ClipManager.OnClipChangeListener l = (ClipManager.OnClipChangeListener )i$.next();
            l.onClipChange(this, vb);
        }

    }

    public void setLastClipSelected(boolean value) {
        if (!this.videos.isEmpty()) {
            VideoBean vb = (VideoBean)this.videos.get(this.videos.size() - 1);
            vb.setState(value ? State.SELECTED : State.READY);
            this.dispatchClipChange(vb);
        }
    }

    public boolean isLastClipMinTime() {
        return this.getLastClipTime() >= 0L && this.getLastClipTime() <= 200L;
    }

    public boolean isReachJumpTime() {
        return this.getRemainingDuration() <= 200;
    }

    public interface OnClipChangeListener {
        void onClipChange(ClipManager var1, VideoBean var2);
    }

    public interface Listener {
        void onClipListChange(ClipManager var1);

        void onClipCreate(ClipManager var1, VideoBean var2);

        void onClipDelete(ClipManager var1, VideoBean var2);
    }
}

